<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *上传图片 
 *本地上传，提交到指定的51帐号的相册中
 *@return
 *	0成功，1上传出错，2文件超出大小，3不是指定文件类型
 */
function uploadImg($key,$sizeCFG='') {
	if(empty($key)){
		return array('stu'=>4,'msg'=>'key为空！');
	}
	$_type = $_FILES[$key]['type'];
	$_size = $_FILES[$key]['size'];
	if($_FILES[$key]['error'] > 0){
		return array('stu'=>1,'msg'=>'文件上传失败！');
	}
	if(empty($sizeCFG)){
		if($_size > 200000){//200K
			return array('stu'=>2,'msg'=>'文件大小超过限制，最大200K！');
		}
	}else{
		if($_size > $sizeCFG){
			return array('stu'=>2,'msg'=>'文件大小超过限制！');
		}
	}
	switch ($_type){
		case 'image/jpeg':
		case 'image/pjpeg':
		case 'image/gif':
		case 'image/png':
        case 'image/x-png':
		case 'image/bmp':
			$_f = 1;
		break;
		default:
			$_f = 0;
		break;
	}
	if($_f == 0){
		return array('stu'=>3,'msg'=>'文件类型不对！');
	}
	if(is_uploaded_file($_FILES[$key]['tmp_name'])){
        $fileName = md5($_FILES[$key]['tmp_name'].time());
        $fileInfo = pathinfo($_FILES[$key]["name"]);
        //$filePath = '/app/nginx/wwwroot/dmz.51.com/pic/'.$fileName.'.'.$fileInfo['extension'];
        $filePath = '/opt/wwwroot/xx5/act.xx5.com/wwwroot/2016/bbpx2016/upload/'.$fileName.'.'.$fileInfo['extension'];
        if(move_uploaded_file($_FILES[$key]['tmp_name'],$filePath)){
            //$_url ='http://video.xx5.com/pic/'.$fileName.'.'.$fileInfo['extension'];
            $_url ='http://act.xx5.com/2016/bbpx2016/upload/'.$fileName.'.'.$fileInfo['extension'];
            return array('stu'=>0,'msg'=>$_url);
        }else{
            return array('stu'=>1,'msg'=>'文件上传出错！');
        }
	}else{
		return array('stu'=>1,'msg'=>'文件上传出错！');
	}
}

function _uploadStream2DFS($user='x5x51234', $data) {
    $tim = time();
    $key = md5('4bea131d1d9ad633825ba6fbe316b25e0ed92218' . $user . $tim);
    $url = '/up_v2/upload_image.php?user=' . $user . '&time=' . $tim . '&key=' . $key . '&from=flash';
    $ret = _upload('csup.51.com', $url, $data);
    //var_dump($ret);
    if (false !== $ret) {
        $img = json_decode($ret, true);
        if ('0' == $img['errno'] && '' != $img['data']) {
            return $img['data'];
        }
        var_dump($img);
    }
    return false;
}

function _delete($user, $image) {
    $tim = time();
    $key = md5('4bea131d1d9ad633825ba6fbe316b25e0ed92218' . $user . $tim);
    $url = 'http://csup.51.com/up_v2/delete_image.php?user=' . $user . '&time=' . $tim . '&key=' . $key . '&url=' . $image;
    $ret = httpRequest($url);
}

function _upload($host, $url, $memo) {
    if ('' == $url || '' == $memo) {
        return false;
    }
    $host_ip = @gethostbyname($host);
    $length = strlen($memo);
    $in = "POST " . $url . " HTTP/1.0\r\n";
    $in .= "Accept: */*\r\n";
    $in .= "Accept-Language: zh-CN\r\n";
    $in .= "x-flash-version: 10,0,12,36\r\n";
    $in .= "Content-Type: application/octet-stream\r\n";
    $in .= "Content-Length: " . $length . "\r\n";
    $in .= "UA-CPU: x86\r\n";
    $in .= "Accept-Encoding: gzip, deflate\r\n";
    $in .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152)\r\n";
    $in .= "Host: " . $host . "\r\n";
    $in .= "Connection: Keep-Alive\r\n";
    $in .= "Cache-Control: no-cache\r\n\r\n";
    $in .= $memo;

    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (!$sock) {
        return false;
    }
    socket_set_nonblock($sock);
    $res = @socket_connect($sock, $host_ip, "80");
    $ret = socket_select($fd_read = array($sock), $fd_write = array($sock), $except = NULL, 0, 500000);
    if (false === $ret) {
        socket_close($sock);
        return false;
    } else if ($ret != 1) {
        socket_close($sock);
        return false;
    }
    socket_set_block($sock);

    @socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2, "usec" => 0));
    @socket_set_option($sock, SOL_SOCKET, SO_SENDIMEO, array("sec" => 2, "usec" => 0));
    $res = socket_write($sock, $in, strlen($in));
    if (false === $res) {
        socket_close($sock);
        return false;
    }
    $line = socket_read($sock, 2048);
    $p = strstr($line, "\r\n\r\n");
    if ($p) {
        $res = substr($p, 4, strlen($p) - 4);
        socket_close($sock);
        if (strstr($res, "404 Not Found")) {
            return false;
        }
        return $res;
    }
    socket_close($sock);
    return true;
}

/**
 * http 请求
 *
 * */
function httpRequest($url, $post_string='', $connectTimeout=5, $readTimeout=10) {
    $result = "";
    $timeout = $connectTimeout + $readTimeout;
    // Use CURL if installed...
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    if (!$post_string) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, '51.com API PHP5 Client 1.1 (curl) ' . phpversion());
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

?>
